package com.joke.praser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class JokePraser {
	static String url = "http://lhy.kync.com/humor2/home/855243bb524e4dabae9b9999f1ddcd8f/2010-1-9/2010-1-10/";

	static String def_url = "http://lhy.kync.com/humor2/home/855243bb524e4dabae9b9999f1ddcd8f/";

	ArrayList<String> urlcache = new ArrayList<String>();

	public static void main(String[] args) {

		JokePraser jp = new JokePraser();
		jp.createUrl();
		InputStream is = jp.loadxml(url);
		jp.praserXML(is);
	}

	public void createUrl() {
		int m = 31;
		for (int i = 1; i < 8; i++) {
			if (i == 1 || i == 3 || i == 5 || i == 7) {
				m = 31;
			} else if (i == 4 || i == 6) {
				m = 30;
			} else {
				m = 28;
			}
			for (int j = 1; j <= m; j++) {
				StringBuffer sb = new StringBuffer("2010-");
				sb.append(i).append("-").append(j);
				sb.append("/2010-");
				if (m == j) {// 月底接月初
					sb.append(i + 1).append("-").append(1);
				} else {
					sb.append(i).append("-").append(j + 1);
				}
				sb.append("/");
				urlcache.add(def_url + sb.toString());
			}

		}

	}

	public InputStream loadxml(String url) {
		return HttpUtils.httpGetIS(url);
	}

	private void praserXML(InputStream is) {
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(is);
			Element element = document.getRootElement();
			List list = element.selectNodes("//jokes/jokenode");
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				Node jokenode = (Node) iter.next();
				System.out.println(jokenode.valueOf("title"));
				System.out.println(jokenode.valueOf("body"));
			}

		} catch (DocumentException e) {
			e.printStackTrace();
		}

	}
}
